<?php
require_once("../DAO/UserDAO.php");
include_once("../DAO/CategoryDAO.php");
include_once("../DAO/TestDAO.php");
include_once("../DAO/TestDetailDAO.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");
include_once("../Entities/Test.php");
include_once("../Entities/TestDetail.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

//http://www.box.net/shared/atj8ukaa9g
if(WebUtilities::GetQueryString("btCreate", null) != null)
{
	$test = new Test();
	$testDAO = new TestDAO();
	$test->Name = WebUtilities::GetQueryString("tbName", "");
	$test->Description = WebUtilities::GetQueryString("tbDescription", "");
	$test->CreatedBy = WebSession::CurrentUserNoReturn()->UserId;
	if(WebUtilities::GetQueryString("rbCreationQuestion", 0) == 0)
	{
		$categoryCount = WebUtilities::GetQueryString("hdCategoryCount", 0);
		for($i = 0; $i < $categoryCount; $i++)
		{
			$categoryId = WebUtilities::GetQueryString("cbCategory" . $i, -1);
			$quantity = WebUtilities::GetQueryString("tbQuantity" . $categoryId, 0);
			$marks = WebUtilities::GetQueryString("tbMarks" . $categoryId, 0);
			if($categoryId != -1)
			{
				$details = $testDAO->CreateDetailsFromCategory($categoryId, $quantity, $marks);
				$test->Details = array_merge($test->Details, $details);
			}
		}
	}
	else
	{
		$detailStr = WebUtilities::GetQueryString("hdQuestionIds", "");
		$detailId = split(",", $detailStr);
		if(count($detailId) != 0)
		{
			for($i = 0 ; $i < count($detailId); $i++)
			{
				$detail = new TestDetail();
				$detail->QuestionId = $detailId[$i]; 
				$detail->Marks = WebUtilities::GetQueryString("hdQuestionMarks" . $detailId[$i], 1);
				$test->Details[] = $detail;
			}
		}
	}
	$testDAO->CreateTest($test);
	WebUtilities::Redirect("TestManagement.php");
}

$page = WebUtilities::GetQueryString("page", 1);
$categoryDao = new CategoryDAO();
$categoryList = $categoryDao->GetAll();
$categoryStr = "";
$categoryUl = "";
for($i = 0; $i < count($categoryList); $i++)
{
	$item = $categoryList[$i];
	$categoryStr = $categoryStr . "<tr>
									<td><input type='checkbox' name='cbCategory" . $i . "' value='" . $item->Id ."' onclick='categoryClick(this)' /> " . $item->Name ."</td>
									<td><input name='tbQuantity". $item->Id ."' id='tbQuantity". $item->Id ."' disabled onblur='checkvalidQuantity(this)'/></td>
									<td><input name='tbMarks" . $item->Id ."' id='tbMarks" . $item->Id ."' disabled onblur='checkvalidMarks(this)'/></td>
								  </tr>";
	$categoryUl = $categoryUl . "<li><a class='expand' onclick=\"openCloseNode(this, 'ulCategory" . $item->Id . "')\"></a><img src='../images/category.png' /><label class='lbNode' title='". $item->Id ."'
								onclick=\"selectedNode(this, true)\">" . $item->Name . "</label></li>
								<input type='hidden' value='1' id='hdMarksCategory" . $item->Id . "'/>
								<ul class='ulCategories' id='ulCategory" . $item->Id ."'></ul>";
} 

$mainContent = "<form enctype='multipart/form-data' action='EditTest.php' method='POST'><div>
					<div class='oneLine'>
						<div class='left'>Name</div>
						<input class='right' name='tbName' />
					</div>
					<div class='oneLine'>
						<div class='left'>Description</div>
						<input class='right' name='tbDescription' />
					</div>
					<div class='oneLine'>
						<div class='left'>Creation Type</div>
						<input type='radio' name='rbCreationQuestion' value='0' id='rbRandom' checked onclick='changeType()'>Random questions</input>
						<input type='radio' name='rbCreationQuestion' value='1' onclick='changeType()'>Customize questions</input>
					</div>
					<div class='oneLine' id='panelRandomQuestion'>
						<fieldset>
						<legend>Choose Categories</legend>
						<table class='choose'>
							<thead>
								<tr>
									<td>Category</td>
									<td>Question Quantities</td>
									<td>Marks per question</td>
								</tr>
							</thead>
							<tbody>
								" . $categoryStr ."
							</tbody>
						</table>
						</fieldset>
					</div>
					<div class='oneLine' id='panelCustomQuestion' style='display:none;'>
						<div class='divTree'>
							<ul class='tree'>
								" . $categoryUl . "
							</ul>
						</div>
						<div class='divCommand'>
							<a><img src='../images/delete.png' title='Remove selected question' onclick='RemoveSelectedQuestion()'></a>
							<a><img src='../images/recycle.png' title='Remove All Questions' onclick='RemoveAllQuestions()'></a>
						</div>
						<div class='rightTable'>
							<div class='oneLine'>
								<div class='left'>Marks per question</div>
								<input class='right' id='tbMarksPerQuestion' name='tbMarksPerQuestion' />
								<input type='button' value='Change' onclick='SaveMarks()' />
							</div><br/>
							<table class='manager' id='tableQuestions'>
								<thead>
									<tr>
										<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
										<th>Question Id</th>
										<th>Content</th>
										<th>Sound</th>
									</tr>
								</thead>
								<tbody id='tableQuestionsBody'>
									<tr>
										<td colspan='4'>No Items</td>
									</tr>
								</tbody>
							</table>
							<input type='hidden' value='0' id='hdCurrentCategory' />
							<input type='hidden' value='-1' id='hdSelectedQuestion' />
							<input type='hidden' value='1' id='hdCurrentPage' />
							<input type='hidden' value='' id='hdQuestionIds' name='hdQuestionIds' />
							<input type='hidden' value='" . count($categoryList) ."' name='hdCategoryCount' />
							<div class='paging' id='divPaging'>
							</div>
							<div class='paging'>
								<input type='button' value='Add' onclick='AddQuestion2Category()' />
							</div>
						</div>
					</div>
					<div class='oneLine'>
						<input type='submit' value='Create' name='btCreate' />
						<input type='button' value='Cancel' onclick=\"window.location = 'TestManagement.php?page=" . $page . "'\" />
					</div>
				</div>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'></div>
				</div></form>";
$pageIndex = 3;
include("MasterPage.php");
?>